/**
 *Class CreateUserScreen
 *
 * version 1.0
 *
 * @author Jin Kit Chua
 * @version 20130319 V1.0
 */
package ch.zbw.iCoach.kit;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.zbw.iCoach.remo.MainLogic;

public class CreateUserScreen {

	private JFrame mainFrame;

	private JButton create;
	private JButton cancel;

	private JTextField username;
	private JTextField firstname;
	private JTextField lastname;

	private JLabel user;
	private JLabel first;
	private JLabel last;

	private JPanel buttonPanel;
	private JPanel userPanel;

	private MainLogic ml;
	private Languages la;

	/**
	 * 
	 * @param lm reference to the mainlogic to use the interface
	 * @param la reference for translation
	 */
	public CreateUserScreen(MainLogic lm, Languages la) {
		this.ml = lm;
		this.la = la;

		this.mainFrame = new JFrame("Create User");

		this.create = new JButton("create User");
		this.cancel = new JButton("cancel");

		this.username = new JTextField();
		this.firstname = new JTextField();
		this.lastname = new JTextField();
		this.username.setEditable(true);
		this.firstname.setEditable(true);
		this.lastname.setEditable(true);

		this.user = new JLabel("User name");
		this.first = new JLabel("First name");
		this.last = new JLabel("Last name");

		this.buttonPanel = new JPanel();
		this.userPanel = new JPanel();

		paint();
	}

	public void paint() {

		this.mainFrame.setBackground(Color.lightGray);
		this.mainFrame.setResizable(false);

		String tempLang = this.la.switchLangCreateUser();
		String[] parts = tempLang.split(",");
		String user = parts[0];
		String fst = parts[1];
		String lst = parts[2];
		String buttonCreate = parts[3];
		String buttonCancel = parts[4];
		String title = parts[5];

		this.user.setText(user);
		this.first.setText(fst);
		this.last.setText(lst);
		this.create.setText(buttonCreate);
		this.cancel.setText(buttonCancel);
		this.mainFrame.setTitle(title);

		this.userPanel.setLayout(new GridLayout(3, 2, 20, 20));
		this.mainFrame.setLayout(new BorderLayout(10, 20));
		this.buttonPanel.setLayout(new GridLayout(1, 2, 20, 20));

		this.buttonPanel.add(this.create);
		this.buttonPanel.add(this.cancel);

		this.userPanel.add(this.user);
		this.userPanel.add(this.username);
		this.userPanel.add(this.first);
		this.userPanel.add(this.firstname);
		this.userPanel.add(this.last);
		this.userPanel.add(this.lastname);

		this.mainFrame.add(buttonPanel, BorderLayout.SOUTH);
		this.mainFrame.add(userPanel, BorderLayout.NORTH);

		this.mainFrame.setSize(400, 250);
		this.mainFrame.setVisible(true);
		this.mainFrame.setLocation(650, 400);

		this.create.addActionListener(new ActionListenerCreate());
		this.cancel.addActionListener(new ActionListenerCancel());
	}

	class ActionListenerCreate implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String msg;
			msg = ml.addUser(firstname.getText(), lastname.getText(),
					username.getText());
			createMessage(msg);
			ml.update();
			mainFrame.dispose();
		}
	}

	class ActionListenerCancel implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			mainFrame.dispose();
		}
	}

	public void createMessage(String msg) {
		Message infoBox = new Message(msg);
		infoBox.paint();
	}
}
